using System.Text;

namespace ThingsGateway.Foundation
{

    public static class LogNetExtensions
    {

        public static void Debug(this ILogNet logger, string keyword, Exception ex)
        {
            logger?.Debug(keyword, GetStringFromException(keyword, ex));
        }

        public static void Debug(this ILogNet logger, Exception ex)
        {
            logger?.Debug(null, GetStringFromException(null, ex));
        }

        public static void Debug(this ILogNet logger, string keyword, string msg)
        {
            logger?.Log(LogType.Debug, keyword, msg);
        }

        public static void Debug(this ILogNet logger, string msg)
        {
            logger?.Debug(null, msg);
        }

        public static void Error(this ILogNet logger, string keyword, Exception ex)
        {
            logger?.Error(keyword, GetStringFromException(keyword, ex));
        }

        public static void Error(this ILogNet logger, Exception ex)
        {
            logger?.Error(null, GetStringFromException(null, ex));
        }

        public static void Error(this ILogNet logger, string keyword, string msg)
        {
            logger?.Log(LogType.Error, keyword, msg);
        }

        public static void Error(this ILogNet logger, string msg)
        {
            logger?.Error(null, msg);
        }

        public static void Fatal(this ILogNet logger, string keyword, Exception ex)
        {
            logger?.Fatal(keyword, GetStringFromException(keyword, ex));
        }
        public static void Fatal(this ILogNet logger, Exception ex)
        {
            logger?.Fatal(null, GetStringFromException(null, ex));
        }
        public static void Fatal(this ILogNet logger, string keyword, string msg)
        {
            logger?.Log(LogType.Fatal, keyword, msg);
        }
        public static void Fatal(this ILogNet logger, string msg)
        {
            logger?.Fatal(null, msg);
        }
        public static void Info(this ILogNet logger, string keyword, Exception ex)
        {
            logger?.Info(keyword, GetStringFromException(keyword, ex));
        }
        public static void Info(this ILogNet logger, Exception ex)
        {
            logger?.Info(null, GetStringFromException(null, ex));
        }
        public static void Info(this ILogNet logger, string keyword, string msg)
        {
            logger?.Log(LogType.Info, keyword, msg);
        }
        public static void Info(this ILogNet logger, string msg)
        {
            logger?.Info(null, msg);
        }
        public static void Trace(this ILogNet logger, string keyword, Exception ex)
        {
            logger?.Trace(keyword, GetStringFromException(keyword, ex));
        }
        public static void Trace(this ILogNet logger, Exception ex)
        {
            logger?.Trace(null, GetStringFromException(null, ex));
        }
        public static void Trace(this ILogNet logger, string keyword, string msg)
        {
            logger?.Log(LogType.Trace, keyword, msg);
        }
        public static void Trace(this ILogNet logger, string msg)
        {
            logger?.Trace(null, msg);
        }
        public static void Warn(this ILogNet logger, string keyword, Exception ex)
        {
            logger?.Warn(keyword, GetStringFromException(keyword, ex));
        }
        public static void Warn(this ILogNet logger, Exception ex)
        {
            logger?.Warn(null, GetStringFromException(null, ex));
        }
        public static void Warn(this ILogNet logger, string keyword, string msg)
        {
            logger?.Log(LogType.Warn, keyword, msg);
        }
        public static void Warn(this ILogNet logger, string msg)
        {
            logger?.Warn(null, msg);
        }

        internal static string GetDegreeDescription(LogType degree)
        {
            switch (degree)
            {
                case LogType.None:
                    return Resource.LogAbandon;

                case LogType.Fatal:
                    return Resource.LogFatal;

                case LogType.Error:
                    return Resource.LogError;

                case LogType.Warn:
                    return Resource.LogWarn;

                case LogType.Info:
                    return Resource.LogInfo;

                case LogType.Debug:
                    return Resource.LogDebug;
                case LogType.Trace:
                    return Resource.LogTrace;

                default:
                    return Resource.LogAbandon;
            }
        }

        internal static string GetStringFromException(string text, Exception ex)
        {
            StringBuilder stringBuilder = new StringBuilder(Environment.NewLine);
            if (ex != null)
            {
                try
                {
                    stringBuilder.Append("\u0002/========[    Exception " + text + "   ]========/");
                    stringBuilder.Append(Environment.NewLine);
                    stringBuilder.Append(Resource.LogExceptionMessage);
                    stringBuilder.Append(ex.Message);
                    stringBuilder.Append(Environment.NewLine);
                    stringBuilder.Append(Resource.ExceptionSource);
                    stringBuilder.Append(ex.Source);
                    stringBuilder.Append(Environment.NewLine);
                    stringBuilder.Append(Resource.ExceptionStackTrace);
                    stringBuilder.Append(ex.StackTrace);
                    stringBuilder.Append(Environment.NewLine);
                    stringBuilder.Append(Resource.ExceptionType);
                    stringBuilder.Append(ex.GetType().ToString());
                    stringBuilder.Append(Environment.NewLine);
                    stringBuilder.Append(Resource.ExceptionTargetSite);
                    stringBuilder.Append(ex.TargetSite?.ToString());
                }
                catch
                {
                }
                stringBuilder.Append(Environment.NewLine);
                stringBuilder.Append("\u0002/========[    Exception    ]========/");
            }
            return stringBuilder.ToString();
        }

    }
}